import dayjs from 'dayjs'
import { mask } from 'shiptools';
import { isEqual } from "@ngard/tiny-isequal";
import { getBirthAndSex, validator } from './index'
import isSameOrBefore from './dayjs_plugin/isSameOrBefore'
import isSameOrAfter  from './dayjs_plugin/isSameOrAfter'
dayjs.extend(isSameOrBefore)
dayjs.extend(isSameOrAfter)

import { 
    SHIP_STUDENT_SUPPLIERS, 
    DEFAULT_STUDENT_SET, 
    SHIP_PASSENGER_SEX, 
    SHIP_NATION_TYPE,
    CERT_TIP_MSG,
    CERT_REG,
    CERT_TIP_IMG,
    SHIP_REG,
    SHIP_VISA_TYPE,
    SHIP_CERT_TYPE
} from './constants'
// 校验证件 
/**
 * 校验证件  输出符合要求的证件列表
 * @param p 
 * @param certTypes 
 * @param certs 
 * @param first 
 */
function _exportValidCert(p, certTypes, certs, first) {
    let validCert = []
    let errMsg = ''
    certTypes.forEach(firstCert => {

        const listNos = p.listNos || []

        // 当前常旅 符合要求的 证件
        const firstCertFilter = listNos.filter((c) => c.certType === Number(firstCert) && c.certNo)
        // if(firstCertFilter.length <= 0) {
        //     invalidMsg.push("第一证件不满足")
        //     return
        // }
        // console.log(`【${fullname}】的 ${SHIP_CERT_TYPE[firstCert] } 有效 `)

        
        const  setttingByCert = certs[firstCert]

        const cardIdP = firstCertFilter.filter(item => item.certType === 1 && item.certNo)
        firstCertFilter.forEach(fFilter => {
            // 身份证 性别 生日 (证件有效期 无效置空)
            fFilter.certNoMask = fFilter.certNo
            if(Number(fFilter.certType) === 1 && fFilter.certNo) {
                // 当证件包含 身份证
                if(validator.isValid(fFilter.certNo)) {
                    const certInfo = getBirthAndSex(fFilter.certNo)
                    p.birthday = certInfo.birthDay
                    p.sex = certInfo.sex
                }
                
                fFilter.certNoMask = mask(fFilter.certNo, 6, 14)
                /* 
                * 号码有效时返回内容示例:
                * {
                *   'addrCode': 100101, //地址码信息,
                *   'addr':     '北京市东城区', //地址信息, 只在实例化时传入了GB2260时返回
                *   'birth':    '1988-01-20', //出生年月日
                *   'sex':      1, //性别，0为女，1为男
                *   'checkBit': 'X', //校验位，仅当18位时存在
                *   'length':   18 //身份证类型，15位或18位
                * }
                */
            }
            
            fFilter.certActiveTime = dayjs(fFilter.certActiveTime).isAfter(dayjs())? fFilter.certActiveTime: ''

                    
            // 校验 当前证件的 配置 （中文姓名，英文性,英文名, 护照有效期, 护照签发时间, 签发次数）
            let validRelation = []
            setttingByCert.map(relation => {

                // 1: '全部',
                // 2: '中文姓名',
                // 3: '英文性,英文名',
                // 4: '护照有效期',
                // 5: '护照签发时间',
                // 6: '签发次数'
                switch(Number(relation)) {
                    case 1:
                        validRelation.push(relation)
                        break;
                    case 2:
                        if(p.linkerName) {
                            validRelation.push(relation)
                        }
                        break;
                    case 3:
                        if(p.englishXin && p.englishMing) {
                            validRelation.push(relation)
                        }
                        break;
                    case 4:
                        if(Number(fFilter.certType) === 2 && dayjs(fFilter.certActiveTime).isAfter(dayjs())) {
                            validRelation.push(relation)
                        }
                        break;
                    case 5:
                        if(dayjs(p.issueDate).isBefore(dayjs())) {
                            validRelation.push(relation)
                        }
                        break;
                    case 6:
                        if(p.issueNum > 0) {
                            validRelation.push(relation)
                        }
                        break;
                }
            })
            // console.log(`fFilter.【${p.linkerName} ${p.englishMing}-${p.englishXin}】 证件【${fFilter.certType}】isDefault:`, fFilter.isDefault)
            // FIXME: 当前证件 验证通过
            let isDefault = first? fFilter.isDefault === 1: true
            const certValid = CERT_REG[fFilter.certType].test(fFilter.certNo)
            if(certValid) {
                validCert.push(fFilter)
                // return validCert
            }

            if(!isEqual(validRelation, setttingByCert)) {
                errMsg = `证件的配置信息不满足${setttingByCert}`
            }
            
        })

    })

    return {
        validCert,
        errMsg
    }
}


// 构建表单
export function buildForm(category, settings, supplierId, _editForm, isEdit) {
    
    if(!settings || !supplierId) {
        return []
    }
    if(category && !SHIP_STUDENT_SUPPLIERS.includes(supplierId)) {
        settings = DEFAULT_STUDENT_SET
    }

    const sexTypes = Object.values(SHIP_PASSENGER_SEX)
    const yearsBefore = dayjs().subtract(100, 'year').format('YYYY-MM-DD')
    const yearsAfter = dayjs().add(100, 'year').format('YYYY-MM-DD')
    const nowDate = dayjs().format('YYYY-MM-DD')
    let formItems = []
    // 获取配置的国籍列表
    const setNations = Object.keys(settings)
    // const _editForm = editForm

    // 设置默认国籍d
    let defaultNation = _editForm.nationType || setNations[0]
    let nationSpec = _editForm.nationalityDetail
    let firstCertType = _editForm.firstCertType
    let defaultFirstNo = _editForm.firstCertNo
    let secondCertType = _editForm.secondCertType
    let defaultSecondNo = _editForm.secondCertNo
    let defaultChineseName = _editForm.linkerName
    let defaultEnglishXing = _editForm.englishXin
    let defaultEnglishMing = _editForm.englishMing
    let passportDate = _editForm.passportDate && dayjs(_editForm.passportDate).isSameOrAfter(dayjs(), 'day') && dayjs(_editForm.passportDate).isSameOrBefore(yearsAfter)? dayjs(_editForm.passportDate).format('YYYY-MM-DD'): ''
    let passportSignDate = _editForm.issueDate && dayjs(_editForm.issueDate).isSameOrBefore(dayjs(), 'day') && dayjs(_editForm.issueDate).isSameOrAfter(yearsBefore)? dayjs(_editForm.issueDate).format('YYYY-MM-DD'): ''
    let passportSignTimes = _editForm.issueNum
    let sex = _editForm.sex
    let birth = _editForm.birthday
    let visaDate = _editForm.visaDate
    let visaNum = _editForm.visaNum
    let stayDays = _editForm.stayDays
    let visaType = _editForm.visaType

    // 额外信息配置
    let defaultNationSetting = settings[defaultNation]
    if(!defaultNationSetting) {
        defaultNation = setNations[0]
        defaultNationSetting = settings[defaultNation]
    }
    const LinkerExtra = defaultNationSetting.LinkerExtra
    const firstCertSetting = Object.keys(defaultNationSetting.FirstCerts)
    const secondCertSetting = Object.keys(defaultNationSetting.SecondCerts)
    const nationBol = setNations.length > 0 && setNations.indexOf('1') === -1
    const nationDetailBol = defaultNation === '6'
    // 有国籍配置，并且不是全部，展示国籍选项
    if (nationBol) {
        formItems.push({
            id: 1,
            name: 'nationality',
            required: true,
            title: '国籍',
            disabled: false,
            defaultValue: setNations.indexOf(defaultNation),
            selectorValue: SHIP_NATION_TYPE[defaultNation],
            placeholder: '请选择国籍',
            data: setNations.map(item => SHIP_NATION_TYPE[item]),
            type: 'picker',
            mode: 'selector',
            reg: SHIP_REG.CHINESE,
            icon: 'iconxia',
        })
    }

    // 国籍是外籍时显示 具体国籍选项
    if (nationDetailBol) {
        formItems.push({
            id: 2,
            name: 'nationalityDetail',
            required: true,
            title: '具体国籍',
            defaultValue: nationSpec,
            selectorValue: nationSpec,
            placeholder: '请选择具体国籍',
            disabled: true,
            data: [],
            type: 'page',
            url: `../nations/index`,
            mode: '',
            reg: SHIP_REG.CHINESE,
            icon: 'iconyou',
        })
    }

    // 默认证件类型
    let defaultFirstCert = firstCertType && defaultNationSetting.FirstCerts[firstCertType]? firstCertType: Number(firstCertSetting[0])

    // 证件配置
    const defaultFirstCertInfoSetting = defaultNationSetting.FirstCerts[defaultFirstCert]
    const chineseNameBol = defaultFirstCertInfoSetting.indexOf('2') > -1
    const englishNameBol = defaultFirstCertInfoSetting.indexOf('3') > -1
    const passportValidityBol = defaultFirstCertInfoSetting.indexOf('4') > -1
    const passportSignBol = defaultFirstCertInfoSetting.indexOf('5') > -1
    const passportFrequencyBol = defaultFirstCertInfoSetting.indexOf('6') > -1

    if(category) {
        // formItems.push({
        //     id: -1,
        //     name: 'linkerType',
        //     required: false,
        //     title: '乘客类型',
        //     disabled: false,
        //     defaultValue: 0,
        //     selectorValue: '学生票',
        //     placeholder: '请选择乘客类型',
        //     data: [2].map(item => SHIP_PASSENGER_TYPE[item]),
        //     type: 'picker',
        //     mode: 'selector',
        //     reg: SHIP_REG.CHINESE,
        //     icon: ''
        // })
    }

    if(firstCertSetting && firstCertSetting.length > 0) {

        // 根据证件类型 去证件号码 reg
        let showCertTip = CERT_TIP_MSG[defaultFirstCert]
        let certNoRegTest = CERT_REG[defaultFirstCert];
        if(Number(defaultFirstCert) === 1 && !validator.isValid(defaultFirstNo)) {
            certNoRegTest = new RegExp('false')
        }
        formItems.push({
            id: 6,
            name: 'firstCertType',
            required: false,
            title: '证件类型',
            disabled: false,
            defaultValue: firstCertSetting.indexOf(defaultFirstCert + ''),
            selectorValue: defaultFirstCert,
            placeholder: '请选择证件类型',
            data: firstCertSetting.map(item => SHIP_CERT_TYPE[item]),
            type: 'picker',
            mode: 'selector',
            reg: SHIP_REG.CHINESE,
            icon: 'iconxia',
            remindTitle: '',
            remind: '',
            remindImg: '',
        },{
            id: 7,
            name: 'firstCertNo',
            required: true,
            title: '证件号码',
            disabled: false,
            defaultValue: defaultFirstNo,
            selectorValue: defaultFirstNo,
            placeholder: '请输入证件号码',
            data: [],
            type: 'input',
            mode: 'text',
            reg: certNoRegTest,
            icon: 'iconxia',
            remindTitle: SHIP_CERT_TYPE[defaultFirstCert],
            remind: showCertTip,
            remindImg: CERT_TIP_IMG[defaultFirstCert],
        })

        if (chineseNameBol) {
            formItems.push({
                id: 3,
                name: 'linkerName',
                required: true,
                // error: nameErrMsg,
                title: '姓名',
                disabled: false,
                tip: 'iconzhuyi1',
                defaultValue: defaultChineseName,
                selectorValue: defaultChineseName,
                placeholder: '请输入姓名',
                data: [],
                type: 'input',
                mode: 'text',
                reg: SHIP_REG.CN_NAME,
                icon: 'iconxia',
                remindTitle: '',
                remind: '',
                remindImg: '',
            })
        }
        if (englishNameBol) {
            formItems.push({
                id: 4,
                name: 'englishXin',
                required: true,
                title: '英文姓',
                disabled: false,
                tip: 'iconzhuyi1',
                defaultValue: defaultEnglishXing,
                selectorValue: defaultEnglishXing,
                placeholder: '请输入英文姓 例如：Li',
                data: [],
                type: 'input',
                mode: 'text',
                reg: SHIP_REG.EN_NAME,
                icon: 'iconxia',
                remindTitle: '',
                remind: '',
                remindImg: '',
            }, {
                id: 5,
                name: 'englishMing',
                required: true,
                title: '英文名',
                disabled: false,
                tip: 'iconzhuyi1',
                defaultValue: defaultEnglishMing,
                selectorValue: defaultEnglishMing,
                placeholder: '请输入英文名 例如：Tao',
                data: [],
                type: 'input',
                mode: 'text',
                reg: SHIP_REG.EN_NAME,
                icon: 'iconxia',
                remindTitle: '',
                remind: '',
                remindImg: '',
            })
        }
        
        if (passportValidityBol) {
            if(dayjs(passportDate).isAfter(yearsAfter) || dayjs(passportDate).isBefore(nowDate)) {
                passportDate = ''
            }
            formItems.push({
                id: 10,
                name: 'passportDate',
                required: true,
                title: '护照有效期',
                disabled: false,
                defaultValue: passportDate,
                selectorValue: passportDate,
                placeholder: '请选择护照有效期',
                data: [],
                start: nowDate,
                end: yearsAfter,
                type: 'picker',
                mode: 'date',
                reg: SHIP_REG.DATE,
                icon: 'iconxia',
                remindTitle: '',
                remind: '',
                remindImg: '',
            })
        }
        if (passportSignBol) {
            formItems.push({
                id: 11,
                name: 'issueDate',
                required: true,
                title: '护照签发时间',
                disabled: false,
                defaultValue: passportSignDate,
                selectorValue: passportSignDate,
                placeholder: '请选择护照签发时间',
                data: [],
                start: yearsBefore,
                end: nowDate,
                type: 'picker',
                mode: 'date',
                reg: SHIP_REG.DATE,
                icon: 'iconxia',
                remindTitle: '',
                remind: '',
                remindImg: '',
            })
        }
        if (passportFrequencyBol) {
            formItems.push({
                id: 12,
                name: 'issueNum',
                required: true,
                title: '签发次数',
                disabled: false,
                defaultValue: passportSignTimes,
                selectorValue: passportSignTimes,
                placeholder: '请输入签发次数',
                data: [],
                type: 'input',
                mode: 'number',
                reg: SHIP_REG.NUMBER_LARGER_THAN_ONE,
                icon: 'iconxia',
                remindTitle: '',
                remind: '',
                remindImg: '',
            })
        }
    }
    if (LinkerExtra && LinkerExtra.length > 0) {
            

        const sexBol = LinkerExtra.indexOf('1') > -1
        const birthdayBol = LinkerExtra.indexOf('2') > -1
        const visaValidityBol = LinkerExtra.indexOf('3') > -1

        const visaNoBol = LinkerExtra.indexOf('4') > -1
        const stayDaysBol = LinkerExtra.indexOf('5') > -1
        const visaTypeBol = LinkerExtra.indexOf('6') > -1

        if(visaNoBol) {
            formItems.push({
                id: 20,
                name: 'visaNum',
                required: true,
                title: '签证号码',
                disabled: false,
                defaultValue: visaNum,
                selectorValue: visaNum,
                placeholder: '请输入签证号码',
                data: [],
                type: 'input',
                mode: 'text',
                reg: '',
                icon: 'iconxia',
                remindTitle: '',
                remind: '',
                remindImg: '',
            })
        }

        if(stayDaysBol) {
            formItems.push({
                id: 21,
                name: 'stayDays',
                required: true,
                title: '可停留天数',
                disabled: false,
                defaultValue: stayDays,
                selectorValue: stayDays,
                placeholder: '请输入可停留天数',
                data: [],
                type: 'input',
                mode: 'number',
                reg: SHIP_REG.NUMBER_LARGER_THAN_ONE,
                icon: 'iconxia',
                remindTitle: '',
                remind: '',
                remindImg: '',
            })
        }

        if(visaTypeBol) {
            formItems.push({
                id: 22,
                name: 'visaType',
                required: false,
                title: '签证类型',
                disabled: false,
                defaultValue: SHIP_VISA_TYPE.indexOf(visaType),
                selectorValue: visaType,
                placeholder: '请选择签证类型',
                data: SHIP_VISA_TYPE,
                type: 'picker',
                mode: 'selector',
                reg: '',
                icon: 'iconxia',
                remindTitle: '',
                remind: '',
                remindImg: '',
            })
        }

        // 当前证件类型是身份证时 不展示性别和生日
        if(Number(defaultFirstCert) !== 1) {
            if (sexBol) {
                formItems.push({
                    id: 8,
                    name: 'sex',
                    required: true,
                    title: '性别',
                    disabled: false,
                    defaultValue: sex,
                    selectorValue: sexTypes[sex],
                    placeholder: '请选择性别',
                    data: sexTypes,
                    start: yearsBefore,
                    end: nowDate,
                    type: 'picker',
                    mode: 'selector',
                    reg: SHIP_REG.CHINESE,
                    icon: 'iconxia',
                    remindTitle: '',
                    remind: '',
                    remindImg: '',
                })
            }
            if (birthdayBol) {
                formItems.push({
                    id: 9,
                    name: 'birthday',
                    required: true,
                    title: '出生日期',
                    disabled: false,
                    defaultValue: birth,
                    selectorValue: birth,
                    placeholder: '请选择出生日期',
                    data: [],
                    start: yearsBefore,
                    end: nowDate,
                    type: 'picker',
                    mode: 'date',
                    reg: SHIP_REG.DATE,
                    icon: 'iconxia',
                    remindTitle: '',
                    remind: '',
                    remindImg: '',
                })
            }
        }
        
        if (visaValidityBol) {
            formItems.push({
                id: 15,
                name: 'visaDate',
                required: true,
                title: '签证有效期',
                disabled: false,
                defaultValue: visaDate,
                selectorValue: visaDate,
                placeholder: '请选择签证有效期',
                data: [],
                start: nowDate,
                end: yearsAfter,
                type: 'picker',
                mode: 'date',
                reg: SHIP_REG.DATE,
                icon: 'iconxia',
                remindTitle: '',
                remind: '',
                remindImg: '',
            })
        }
    }
    
    if (secondCertSetting && secondCertSetting.length > 0) {
            
        // 默认证件类型
        let defaultSecondCert =  defaultNationSetting.SecondCerts[secondCertType]? secondCertType: Number(secondCertSetting[0])
        const defaultSecondCertInfoSetting = defaultNationSetting.SecondCerts[defaultSecondCert]
        // 第二证件
        const secondChineseNameBol = defaultSecondCertInfoSetting.indexOf('2') > -1
        const secondEnglishNameBol = defaultSecondCertInfoSetting.indexOf('3') > -1
        const secondPassportValidityBol = defaultSecondCertInfoSetting.indexOf('4') > -1
        const secondPassportSignBol = defaultSecondCertInfoSetting.indexOf('5') > -1
        const secondPassportFrequencyBol = defaultSecondCertInfoSetting.indexOf('6') > -1

        // 根据证件类型 去证件号码 reg
        let showCertTip = CERT_TIP_MSG[defaultSecondCert]
        formItems.push({
            id: 13,
            name: 'secondCertType',
            required: false,
            title: '证件类型',
            disabled: false,
            defaultValue: secondCertSetting.indexOf(defaultSecondCert + ''),
            // defaultValue: defaultSecondCert,
            selectorValue: defaultSecondCert,
            placeholder: '请选择第二证件类型',
            data: secondCertSetting.map(item => SHIP_CERT_TYPE[item]),
            type: 'picker',
            mode: 'selector',
            reg: SHIP_REG.CHINESE,
            icon: 'iconxia',
            remindTitle: '',
            remind: '',
            remindImg: '',
        },{
            id: 14,
            name: 'secondCertNo',
            required: true,
            title: '证件号码',
            disabled: false,
            defaultValue: defaultSecondNo,
            selectorValue: defaultSecondNo,
            placeholder: '请输入第二证件号码',
            data: [],
            type: 'input',
            mode: 'text',
            reg: CERT_REG[defaultSecondCert],
            icon: 'iconxia',
            remindTitle: SHIP_CERT_TYPE[defaultSecondCert],
            remind: showCertTip,
            remindImg: CERT_TIP_IMG[defaultSecondCert],
        })


        if (secondChineseNameBol && !chineseNameBol) {
            formItems.push({
                id: 3,
                name: 'linkerName',
                required: true,
                // error: nameErrMsg,
                title: '姓名',
                disabled: false,
                tip: 'iconzhuyi1',
                defaultValue: defaultChineseName,
                selectorValue: defaultChineseName,
                placeholder: '请输入第二证件姓名',
                data: [],
                type: 'input',
                mode: 'text',
                reg: SHIP_REG.CN_NAME,
                icon: 'iconxia',
                remindTitle: '',
                remind: '',
                remindImg: '',
            })
        }
        if (secondEnglishNameBol && !englishNameBol) {
            formItems.push({
                id: 4,
                name: 'englishXin',
                required: true,
                title: '英文姓',
                disabled: false,
                tip: 'iconzhuyi1',
                defaultValue: defaultEnglishXing,
                selectorValue: defaultEnglishXing,
                placeholder: '请输入第二证件英文姓 例如：Li',
                data: [],
                type: 'input',
                mode: 'text',
                reg: SHIP_REG.EN_NAME,
                icon: 'iconxia',
                remindTitle: '',
                remind: '',
                remindImg: '',
            }, {
                id: 5,
                name: 'englishMing',
                required: true,
                title: '英文名',
                disabled: false,
                tip: 'iconzhuyi1',
                defaultValue: defaultEnglishMing,
                selectorValue: defaultEnglishMing,
                placeholder: '请输入第二证件英文名 例如：Tao',
                data: [],
                type: 'input',
                mode: 'text',
                reg: SHIP_REG.EN_NAME,
                icon: 'iconxia',
                remindTitle: '',
                remind: '',
                remindImg: '',
            })
        }
        
    
        if (secondPassportValidityBol && !passportValidityBol) {
            formItems.push({
                id: 10,
                name: 'certActiveTime',
                required: true,
                title: '证件有效期',
                disabled: false,
                defaultValue: passportDate,
                selectorValue: passportDate,
                placeholder: '请选择第二证件有效期',
                data: [],
                start: nowDate,
                end: yearsAfter,
                type: 'picker',
                mode: 'date',
                reg: SHIP_REG.DATE,
                icon: 'iconxia',
                remindTitle: '',
                remind: '',
                remindImg: '',
            })
        }
        if (secondPassportSignBol && !passportSignBol) {
            formItems.push({
                id: 11,
                name: 'issueDate',
                required: true,
                title: '护照签发时间',
                disabled: false,
                defaultValue: passportSignDate,
                selectorValue: passportSignDate,
                placeholder: '请选择第二证件签发时间',
                data: [],
                start: yearsBefore,
                end: nowDate,
                type: 'picker',
                mode: 'date',
                reg: SHIP_REG.DATE,
                icon: 'iconxia',
                remindTitle: '',
                remind: '',
                remindImg: '',
            })
        }
        if (secondPassportFrequencyBol && !passportFrequencyBol) {
            formItems.push({
                id: 12,
                name: 'issueNum',
                required: true,
                title: '签发次数',
                disabled: false,
                defaultValue: passportSignTimes,
                selectorValue: passportSignTimes,
                placeholder: '请输入第二证件签发次数',
                data: [],
                type: 'input',
                mode: 'number',
                reg: SHIP_REG.NUMBER_LARGER_THAN_ONE,
                icon: 'iconxia',
                remindTitle: '',
                remind: '',
                remindImg: '',
            })
        }
    }
    // 根据id排序
    formItems.sort((a, b) => {
        return a.id - b.id
    })
    // this.formItems = formItems
    return formItems
}



/**
 * 校验常旅
 * @param p 当前选中的常旅
 * @param linkerSetting 常旅配置
 * @param certValid Boolean是否允许设置默认证件
 */
export function compareForm(p, linkerSetting, supplierId, certValid) {

    // 国籍 p.nationality
    // 国籍 x => 证件 p.listNos.map(item => item.certType)
    // 证件 x 配置项 => p.listNos.filter(item => item.certType === x)
            // 1: '全部',
            // 2: '中文姓名', p.linkerName
            // 3: '英文性,英文名', p.englishXin p.englishMing
            // 4: '护照有效期', p.certTime
            // 5: '护照签发时间', p.issueDate
            // 6: '签发次数' p.issueTimes

    // 国籍 x => 其他配置 
            // 1: '性别', p.sex
            // 2: '出生日期', p.birthday
            // 3: '签证有效期' p.visaDate

    // 所谓校验就是 把 跟当前常旅拥有的信息 与 配置项 进行一一对应 进行比较
    const _linkerName = `【${p.linkerName ? p.linkerName : ''}${p.linkerName && p.englishMing && p.englishXin ? '-' : ''}${p.englishMing && p.englishXin ? (p.englishMing + '/' + p.englishXin) : ''}】`

    // 回参 校验结果
    let invalidMsg = []

    // 学生票 使用默认的常旅配置

    // TODO: 学生票 针对【闽台轮渡】 走常旅配置，其他供应商走正常流程
    
    // console.log("supplierId: ", supplierId)
    if(p.linkerType === 2 && !SHIP_STUDENT_SUPPLIERS.includes(supplierId)) {
        linkerSetting = DEFAULT_STUDENT_SET
    }

    // 提取常旅配置 的  国籍 keys
    let nationTypes = Object.keys(linkerSetting)

    // 默认国籍 之后所有的配置应当在 此 国籍的基础上 才能有效   这里应该 循环国籍项进行比对   如果国籍项只有1 则为全部
    // let defaultNation = nationTypes[0]

    // 常旅全名
    let fullname = `${p.linkerName ? p.linkerName : p.englishMing && p.englishXin ? (p.englishMing + '-' + p.englishXin) : '未知用户'}`;
    

    // console.log("fullname: ", fullname)

    // 根据当前常旅国籍 如果没有就用配置项第一个国籍
    let defaultNation = Number(nationTypes[0])
    nationTypes.map(nation => {
        if (SHIP_NATION_TYPE[nation] === p.nationality) {
            defaultNation = Number(nation)
        }
    })
    if(p.nationType) {
        defaultNation = p.nationType
    }
    
    // 获取 基于国籍的 配置项
    const settingByNation = linkerSetting[defaultNation]

    // 国籍校验
    // 全部
    if(defaultNation !== 1) {
        let nationValid = SHIP_NATION_TYPE[defaultNation] === p.nationality
        if (!p.nationality) {
            // console.log(`${_linkerName}没有国籍`)
            invalidMsg.push(`${_linkerName}没有国籍`)
            // return invalidMsg
        } else if(!nationValid) {
            // console.log("有国籍，但不符合配置要求")
            invalidMsg.push("有国籍，但不符合配置要求")
            // return invalidMsg
        }
    }

    // 第一证件
    const FirstCerts = settingByNation.FirstCerts
    let firstCertTypes = Object.keys(FirstCerts)
    // let validFirstCert = null
    
    const { validCert, errMsg }  = _exportValidCert(p, firstCertTypes, FirstCerts, true)
    const validFirstCerts = validCert
    if(errMsg) {
        invalidMsg.push(errMsg)
    }
    let validFirstCert = null
    // console.log("validFirstCert: ", validFirstCert)
    let firstCertRelation = []
    if(validFirstCerts && validFirstCerts.length > 0) {
        validFirstCerts.sort((a, b) => b.isDefault - a.isDefault)
        validFirstCert = validFirstCerts [0]
        firstCertRelation = FirstCerts[validFirstCert.certType]
        if(certValid) {
            p.nationType = defaultNation
            p.firstCertType = validFirstCert.certType
            p.firstCertNo = validFirstCert.certNo
            p.stayDays = validFirstCert.stayDays
            p.visaNum = validFirstCert.visaNum
            p.visaType = validFirstCert.visaType
        }
    } else {
        p.firstCertType = firstCertTypes[0]
        firstCertRelation = FirstCerts[firstCertTypes[0]]
        // console.log(`【第一证件】'附加信息不满足`)
        invalidMsg.push(`【第一证件】'附加信息不满足`)
        // return invalidMsg
    }

    p.validFirstCert = validFirstCert
    // 第二证件
    const SecondCerts = settingByNation.SecondCerts
    const secondCertTypes = Object.keys(SecondCerts)

    let secondCertRelation = []
    if (secondCertTypes && secondCertTypes.length > 0) {
        const { validCert, errMsg } = _exportValidCert(p, secondCertTypes, SecondCerts, false)
        const validSecondCerts = validCert
        if(errMsg) {
            invalidMsg.push(errMsg)
        }
        let validSecondCert = null
        if(validSecondCerts && validSecondCerts.length > 0) {
            validFirstCerts.sort((a, b) => b.isDefault - a.isDefault)
            validSecondCert = validSecondCerts[0]
            secondCertRelation = SecondCerts[validSecondCert.certType]
            if(certValid) {
                p.secondCertType = validSecondCert.certType
                p.secondCertNo = validSecondCert.certNo
                p.stayDays = validSecondCert.stayDays
                p.visaNum = validSecondCert.visaNum
                p.visaType = validSecondCert.visaType
            }
        } else {
            // console.log(`【第二证件】'附加信息不满足`)
            p.secondCertType = secondCertTypes[0]
            invalidMsg.push(`【第二证件】'附加信息不满足`)
            // return invalidMsg
        }
            
        p.validSecondCert = validSecondCert
    }
    // 额外信息
    const LinkerExtra = settingByNation.LinkerExtra
    // 其他信息 校验
    let currentExtraInfo = []
    if (LinkerExtra && LinkerExtra.length > 0) {
        LinkerExtra.map(item => {
            switch(Number(item)) {
                case 1:
                    if(p.sex === 0 || p.sex === 1) {
                        currentExtraInfo.push(item)
                    }
                    break;
                case 2:
                    if (p.birthday) {
                        currentExtraInfo.push(item)
                    }
                    break;
                case 3: 
                    if (p.visaDate) {
                        currentExtraInfo.push(item)
                    }
                    break;
                case 4:
                    if(p.visaNum) {
                        currentExtraInfo.push(item)
                    }
                    break;
                case 5:
                    if(p.stayDays > 0) {
                        currentExtraInfo.push(item)
                    }
                    break;
                case 6:
                    if(p.visaType) {
                        currentExtraInfo.push(item)
                    }
                    break;
            }
        })
        if (!isEqual(LinkerExtra, currentExtraInfo)) {
            // console.log(`其他信息不完善`)
            invalidMsg.push("其他信息不完善")
            // return invalidMsg
        }
    }
    
    // 设置常旅展示名
    let fullNames = [] 
    let showFullNames = [] // 显示的 名字
    let cnName = firstCertRelation.indexOf('2') > -1 || secondCertRelation.indexOf('2') > -1 ? p.linkerName : ''
    if (cnName) {
        fullNames.push(cnName)
        showFullNames.push(cnName)
    }
    let enName = firstCertRelation.indexOf('3') > -1 || secondCertRelation.indexOf('3') > -1 ? (p.englishXin + '/' + p.englishMing) : ''
    if (enName && p.englishXin && p.englishMing) {
        fullNames.push(enName)
    }

    let enNameNoLine = firstCertRelation.indexOf('3') > -1 || secondCertRelation.indexOf('3') > -1 ? (p.englishXin + '-' + p.englishMing) : ''
    if(enNameNoLine && p.englishXin && p.englishMing) {
        showFullNames.push(enNameNoLine)
    }
    
    fullname = fullNames.join('-')

    // 只有英文姓名的情况下  名字前面要个 -  
    if(!cnName && enName) {
        fullname = `-${enName}`
    }
    p.fullname = fullname

    p.showName = showFullNames.length > 0? showFullNames.join(' '): `${p.linkerName}${p.englishMing? ((p.linkerName && ' ') + p.englishMing): ''}${p.englishXin? ('-' + p.englishXin): ''}`
    console.log("invalidMsg: ", invalidMsg)
    p.isValid = true
    if(invalidMsg.length > 0) {
        p.isValid = false
    }

    // console.log(fullname, invalidMsg)

    return {
        passenger: p, 
        invalidMsg
    }

}

/**
 * 获取证件类型 by 国籍
 * @param nation 国籍类型
 */
function getCertByNation(nation, settings, editForm) {
    const setNations = Object.keys(settings)
    let defaultNationSetting = settings[nation]
    if(!defaultNationSetting) {
        if(setNations.indexOf('1') > -1) { 
            defaultNationSetting = settings[setNations[0]]
        }
    }
    if(!defaultNationSetting) return;
    const LinkerExtra = defaultNationSetting.LinkerExtra
    const firstCertSetting = Object.keys(defaultNationSetting.FirstCerts)
    const secondCertSetting = Object.keys(defaultNationSetting.SecondCerts)
    editForm.firstCertType = firstCertSetting[0]
    editForm.secondCertType = secondCertSetting[0]
    // this.firstCerts = firstCertSetting
    // this.secondCerts = secondCertSetting

    return editForm
}

/**
 * 设置编辑常旅
 * @param passenger 当前编辑
 * @param typing 输入 (切换国籍用)
 */
export function setEditForm(passenger, settings, typing, restNo, editForm) {
    let _editForm = Object.assign({}, editForm)
    if(passenger.nationality && passenger.nationality !== '全部' && !passenger.nationType) {
        const nationIndex = Object.values(SHIP_NATION_TYPE).indexOf(passenger.nationality)
        passenger.nationType = Object.keys(SHIP_NATION_TYPE)[nationIndex]
    }
    if(passenger.nationType) {
        _editForm.nationType = passenger.nationType
    }
    // 证件类型是根据国籍来获取的
    if(!typing) {
        _editForm = getCertByNation(_editForm.nationType, settings, editForm)
    }
    if(passenger.id) {
        // 重置证件号码
        let defaultFirstCert = passenger.firstCertType
        let defaultSecondCert = passenger.secondCertType
        
        if(!typing) {
            defaultFirstCert = _editForm.firstCertType
            defaultSecondCert = _editForm.secondCertType
        }
        // passenger.issueDate = passenger.issueDate
        // passenger.birthday = passenger.birthday
        // passenger.visaDate = passenger.visaDate
        if(passenger.listNos && passenger.listNos.length > 0) { 
            if(!typing || restNo) {
                passenger.firstCertNo = ''
                passenger.secondCertNo = ''
                
                passenger.listNos.forEach(item => {
                    // passenger.certActiveTime = passenger.certActiveTime? dayjs(passenger.certActiveTime).format(DATE_FORMAT_STRING): ''
                    if(item.certType === Number(defaultFirstCert)) {
                        passenger.firstCertNo = item.certNo
                        
                        passenger.stayDays = item.stayDays
                        passenger.visaNum = item.visaNum
                        passenger.visaType = item.visaType
                    }
                    if(item.certType === Number(defaultSecondCert)) {
                        passenger.secondCertNo = item.certNo

                        passenger.stayDays = item.stayDays
                        passenger.visaNum = item.visaNum
                        passenger.visaType = item.visaType
                    }
                })
            }
        }
    }
    editForm = Object.assign({}, _editForm, passenger)
    return editForm
}
